package com.product.miaosha.service.impl;

import com.product.miaosha.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.product.common.utils.PageUtils;
import com.product.common.utils.Query;

import com.product.miaosha.dao.MiaoshaActivityDao;
import com.product.miaosha.entity.MiaoshaActivityEntity;
import com.product.miaosha.service.MiaoshaActivityService;


@Service("miaoshaActivityService")
public class MiaoshaActivityServiceImpl extends ServiceImpl<MiaoshaActivityDao, MiaoshaActivityEntity> implements MiaoshaActivityService {
    @Autowired
    private MiaoshaActivityDao miaoshaActivityDao;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String page1 = (String) params.get("page");

        IPage<MiaoshaActivityEntity> goodsList = (IPage<MiaoshaActivityEntity>) redisTemplate.boundHashOps("goodsList").get("list");

        //如果redis中没有数据,并当前页等于1时
        if (goodsList==null && Integer.parseInt(page1)==1){
                IPage<MiaoshaActivityEntity> page = this.page(
                    new Query<MiaoshaActivityEntity>().getPage(params),
                    new QueryWrapper<MiaoshaActivityEntity>()
                );
                redisTemplate.boundHashOps("goodsList").put("list",page);
                System.out.println("数据库查");
                return new PageUtils(page);

        }else{
                System.out.println("缓存命中");
                return new PageUtils(goodsList);
        }

    }

    @Override
    public GoodsVo shopInfo(Integer id) {
        return miaoshaActivityDao.shopInfo(id);
    }

    @Override
    public MiaoshaActivityEntity MiaoShaGoodsInfo(Long gid) {
        return miaoshaActivityDao.MiaoShaGoodsInfo(gid);
    }

}